package com.yjy.mybase.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;

import java.util.Stack;

/**
 * Created by yangjinyang
 */

public class AppManager {
    private static Stack<Activity> activities;

    /**
     * 构造方法私有化
     */
    private AppManager() {
    }

    /**
     * 实例化单例的静态内部类
     */
    private static class AppManagerHolder {
        public static final AppManager INSTANCE = new AppManager();
    }

    /**
     * 获取单一实例
     *
     * @return 单一实例
     */
    public static AppManager getInstance() {
        return AppManagerHolder.INSTANCE;
    }

    /**
     * 添加Activity到堆栈
     *
     * @param activity
     */
    public void addActivity(Activity activity) {
        if (activities == null)
            activities = new Stack<>();
        activities.add(activity);
    }

    /**
     * 结束指定的Activity
     *
     * @param activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null) {
            activities.remove(activity);
            activity.finish();
            activity = null;
        }
    }

    /**
     * 结束指定的Activity
     *
     * @param cls
     */
    public void finishActivity(Class<? extends Activity> cls) {
        int size = activities.size();
        String className = cls.getName();
        for (int i = 0; i < size; i++) {
            Activity activity = activities.get(i);
            if (className.equals(activity.getClass().getName())) {
                finishActivity(activity);
                break;
            }
        }
    }

    /**
     * 结束当前Activity（堆栈中最后一个压入的）
     */
    public void finishTopActivity() {
        Activity activity = activities.lastElement();
        finishActivity(activity);
    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        for (int i = 0; i < activities.size(); i++) {
            if (null != activities.get(i)) {
                activities.get(i).finish();
            }
        }
        activities.clear();
    }

    /**
     * 退出App
     */
    public void exitApp(Context context) {
        finishAllActivity();
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        activityManager.restartPackage(context.getPackageName());

    }

    /**
     * 获取栈顶Activity
     */
    public Activity getTopActivity() {
        Activity activity = activities.lastElement();
        return activity;
    }

    /**
     * 获取Activity栈
     */
    public Stack<Activity> getStack() {
        return activities;
    }

    /**
     * 重启App
     */
    public void rebootApp() {
        Activity activity = getTopActivity();
        Intent intent = activity.getBaseContext().getPackageManager().getLaunchIntentForPackage(activity.getBaseContext().getPackageName());
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        activity.startActivity(intent);
        System.exit(0);
    }

    /**
     * 获取指定Activity
     */
    public <T> T getActivity(Class<? extends Activity> cls) {
        String className = cls.getName();
        for (Activity activity : activities) {
            if (className.equals(activity.getClass().getName()))
                return (T) activity;
        }
        return null;
    }
}
